const mongoose = require('mongoose')

//连接mongo
let options = {
  database:'mongodb://hmy:123456@localhost:27017/we_teach',
  // database:'mongodb://@localhost:27017/we_teach',
  dataConfig:{useNewUrlParser:true,useUnifiedTopology: true}
}
mongoose.connect(options.database,options.dataConfig)
.then(
  ()=>{
    console.log("连接成功")
  }
).catch(err=>{
  console.log("连接失败")
  console.log(err)
})
// 操作表（集合），定义一个schema,每一个schema映射一个表,字段和表一一对应
// 定义scheme
var student_schema = mongoose.Schema({
  user:{
    type:String,
    unique:true//设置唯一索引，index:true为普通索引
  },
  pass:String,
  student:String,
  avatar:String,
  sex:String,
  subject_good:String,
})
//teacher's scheme
var teacher_schema = mongoose.Schema({
  user:{
    type:String,
    unique:true//设置唯一索引，index:true为普通索引
  },
  pass:String,
  teacher:String,
  avatar:String,
  sex:String,
  subject_good:String,
  school_name:String
})

//article's scheme
var article_schema = mongoose.Schema({
  id: String,
  title: String,
  article: Array,
  author: String,
  source: String,
  time: String,
})
// 把scheme转化为model，即创建数据库表模型，操作表teachers表,首字母要大写
// students’ model
var Students = mongoose.model('students',student_schema)

// teacher’ model
var Teachers = mongoose.model('teachers',teacher_schema)
//article's scheme
var Articles = mongoose.model('test_articles',article_schema)
// Articles.collection.dropIndex(id)
// 导出
module.exports={
  Students : Students,
  Teachers : Teachers,
  Articles:Articles
}